package com.guigusuqi.survey.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.guigusuqi.survey.entity.Question;
import com.guigusuqi.survey.entity.UserQuestionnaire;
import com.guigusuqi.survey.mapper.QuestionMapper;
import com.guigusuqi.survey.mapper.UserQuestionnaireMapper;
import com.guigusuqi.survey.service.UserQuestionnaireService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author suqi
 * @since 2022-04-13
 */
@Service
public class UserQuestionnaireServiceImpl extends ServiceImpl<UserQuestionnaireMapper, UserQuestionnaire> implements UserQuestionnaireService
{
    @Resource
    private UserQuestionnaireMapper userQuestionnaireMapper;

    @Resource
    private QuestionMapper questionMapper;

    @Override
    public List<UserQuestionnaire> selectQuestionnaireByOpenid(String openid)
    {
        LambdaQueryWrapper<UserQuestionnaire> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserQuestionnaire::getOpenid,openid);

//        queryWrapper.select(UserQuestionnaire::getOpenid,UserQuestionnaire::getQuestionId,UserQuestionnaire::getUserAnswer,UserQuestionnaire::getQuestionScore);

        List<UserQuestionnaire> userQuestionnaires = userQuestionnaireMapper.selectList(queryWrapper);

        for (UserQuestionnaire userQuestionnaire : userQuestionnaires)
        {
            String questionId = userQuestionnaire.getQuestionId();
            Question question = questionMapper.selectById(questionId);
            if(Objects.isNull(question))
            {
                return null;
            }
            userQuestionnaire.setQuestion(question.getQuestion());
        }
        return userQuestionnaires;
    }

    @Override
    public int selectPublishQuestionnaireCount(String openid)
    {
        int i = userQuestionnaireMapper.selectPublishQuestionnaireCount(openid);
        return i;
    }
}
